package com.dingreading.cloud.web.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.web.entity.NjTicketUser;
import com.dingreading.cloud.web.entity.NjTicketUserLog;
import com.dingreading.cloud.web.service.NjTicketUserLogService;
import com.dingreading.cloud.web.service.NjTicketUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/app/ticketUser")
@Api(tags = "App端-ACEC门票用户功能")
public class AppTickerUserController {

    @Resource
    NjTicketUserService ticketUserService;
    @Resource
    NjTicketUserLogService ticketUserLogService;

    @ApiOperation(value = "获取门票用户信息")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBookLabels(
            @ApiParam(value = "门票用户uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "用户创建时间") @RequestParam(value = "addTime") String addTime
    ) throws Exception {
        Date date = DateUtil.parseDateFromStr(addTime);
        String year = String.valueOf(DateUtil.getYear());

        NjTicketUser user = ticketUserService.getByUidAndAddTime(uid, date, year);
        if (null == user)
            return R.fail("未查询到门票");
        if (null == user.getGetStatus() || !user.getGetStatus().equals(1))
            return R.fail("门票还未领取");

        NjTicketUserLog log = new NjTicketUserLog();
        log.setYear(year);
        log.setUid(user.getUid());
        log.setPhone(user.getPhone());
        log.setName(user.getName());
        log.setCorpName(user.getCorpName());
        log.setType("App扫码验证");
        ticketUserLogService.saveOrUpdate(log);
        return R.ok(user);
    }

    @ApiOperation(value = "设置领取参会证")
    @PostMapping(value = "/setPermitStatus", produces = "application/json; charset=UTF-8")
    public R<Object> setPermitStatus(
            @ApiParam(value = "门票用户uid") @RequestParam(value = "uid") String uid
    ) {
        String year = String.valueOf(DateUtil.getYear());
        NjTicketUser user = ticketUserService.getByUid(uid, year);
        if (null == user)
            return R.fail("未查询到门票");
        if (null == user.getGetStatus() || !user.getGetStatus().equals(1))
            return R.fail("门票还未领取");
        if (user.getPermitStatus().equals(1))
            return R.fail("参会证不能重复领取");

        boolean b = ticketUserService.setPermitStatus(uid);
        if (!b) return R.fail("操作失败");

        NjTicketUserLog log = new NjTicketUserLog();
        log.setYear(year);
        log.setUid(user.getUid());
        log.setPhone(user.getPhone());
        log.setName(user.getName());
        log.setCorpName(user.getCorpName());
        log.setType("App领取参会证");
        ticketUserLogService.saveOrUpdate(log);

        return R.ok("领取成功");
    }

}
